/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.tesoreria.bc;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.tesoreria.be.DetDocupago;

/**
 *
 * @author Administrador
 */
@Stateless
public class DetDocupagoFacade implements DetDocupagoFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(DetDocupago detDocupago) {
        em.persist(detDocupago);
    }

    public void edit(DetDocupago detDocupago) {
        em.merge(detDocupago);
    }

    public void remove(DetDocupago detDocupago) {
        em.remove(em.merge(detDocupago));
    }

    public DetDocupago find(Object id) {
        try {
            return em.find(DetDocupago.class, id);
        } catch (Exception e) {
            return null;
        }
    }

    public List<DetDocupago> findAll() {
        try {
            return em.createQuery("select object(o) from DetDocupago as o").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    @SuppressWarnings("empty-statement")
    public void insercionMasiva(Long codigoDocumento,List<Long> listaCodigo) {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append("Insert Into tes_det_docupago values");
            for(Iterator i = listaCodigo.iterator();i.hasNext();){
                List valores = (List)i.next();
                sb.append("(").append(codigoDocumento.toString()).append(",").append(((Long)valores.get(0)).toString()).append("),");
            }
            sb.replace(sb.length(), sb.length(), ";");
            Query q = em.createNativeQuery(sb.toString());
            q.executeUpdate();
        } catch (Exception ex) {
            System.out.println("error:" + ex.getMessage());
        }
    }
}
